package com.youyi.yychosesdk.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lkx.liandianqinew.R;
import com.youyi.yychosesdk.constant.Key;
import com.youyi.yychosesdk.constant.Type;
import com.youyi.yychosesdk.models.album.AlbumModel;
import com.youyi.yychosesdk.models.album.entity.Photo;
import com.youyi.yychosesdk.result.Result;
import com.youyi.yychosesdk.setting.Setting;
import com.youyi.yychosesdk.ui.PreviewFragment;
import com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter;
import com.youyi.yychosesdk.ui.widget.PressedTextView;
import com.youyi.yychosesdk.utils.Color.ColorUtils;
import com.youyi.yychosesdk.utils.system.SystemUtils;
import java.util.ArrayList;

public class PreviewActivity extends AppCompatActivity implements PreviewPhotosAdapter.OnClickListener, View.OnClickListener, PreviewFragment.OnPreviewFragmentClickListener {
    private static final int UI_ANIMATION_DELAY = 300;
    private PreviewPhotosAdapter adapter;
    private boolean clickDone;
    View decorView;
    private FrameLayout flFragment;
    private int index;
    private boolean isSingle;
    private ImageView ivSelector;
    private int lastPosition = 0;
    private LinearLayoutManager lm;
    private RelativeLayout mBottomBar;
    private final Handler mHideHandler = new Handler();
    private final Runnable mHidePart2Runnable = new Runnable() {
        /* class com.youyi.yychosesdk.ui.PreviewActivity.AnonymousClass1 */

        public void run() {
            SystemUtils instance = SystemUtils.getInstance();
            PreviewActivity previewActivity = PreviewActivity.this;
            instance.systemUiHide(previewActivity, previewActivity.decorView);
        }
    };
    private final Runnable mShowPart2Runnable = new Runnable() {
        /* class com.youyi.yychosesdk.ui.PreviewActivity.AnonymousClass2 */

        public void run() {
            PreviewActivity.this.mBottomBar.setVisibility(View.VISIBLE);
            PreviewActivity.this.mToolBar.setVisibility(View.VISIBLE);
        }
    };
    private FrameLayout mToolBar;
    private boolean mVisible;
    private ArrayList<Photo> photos = new ArrayList<>();
    private PreviewFragment previewFragment;
    private int resultCode = 0;
    private RecyclerView rvPhotos;
    private PagerSnapHelper snapHelper;
    private int statusColor;
    private PressedTextView tvDone;
    private TextView tvNumber;
    private TextView tvOriginal;
    private boolean unable;

    public PreviewActivity() {
        boolean z = true;
        this.isSingle = Setting.count == 1;
        this.unable = Result.count() != Setting.count ? false : z;
        this.clickDone = false;
    }

    public static void start(Activity activity, int i, int i2) {
        Intent intent = new Intent(activity, PreviewActivity.class);
        intent.putExtra(Key.PREVIEW_ALBUM_ITEM_INDEX, i);
        intent.putExtra(Key.PREVIEW_PHOTO_INDEX, i2);
        activity.startActivityForResult(intent, 13);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.SupportActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.decorView = getWindow().getDecorView();
        SystemUtils.getInstance().systemUiInit(this, this.decorView);
        setContentView(R.layout.activity_preview_easy_photos);
        hideActionBar();
        adaptationStatusBar();
        if (AlbumModel.instance == null) {
            finish();
            return;
        }
        initData();
        initView();
    }

    private void adaptationStatusBar() {
        if (Build.VERSION.SDK_INT >= 23) {
            int color = ContextCompat.getColor(this, R.color.easy_photos_status_bar);
            this.statusColor = color;
            if (ColorUtils.isWhiteColor(color)) {
                getWindow().addFlags(67108864);
            }
        }
    }

    private void hideActionBar() {
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.hide();
        }
    }

    private void initData() {
        Intent intent = getIntent();
        int intExtra = intent.getIntExtra(Key.PREVIEW_ALBUM_ITEM_INDEX, 0);
        this.photos.clear();
        if (intExtra == -1) {
            this.photos.addAll(Result.photos);
        } else {
            this.photos.addAll(AlbumModel.instance.getCurrAlbumItemPhotos(intExtra));
        }
        int intExtra2 = intent.getIntExtra(Key.PREVIEW_PHOTO_INDEX, 0);
        this.index = intExtra2;
        this.lastPosition = intExtra2;
        this.mVisible = true;
    }

    private void toggle() {
        if (this.mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            /* class com.youyi.yychosesdk.ui.PreviewActivity.AnonymousClass3 */

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                PreviewActivity.this.mBottomBar.setVisibility(View.GONE);
                PreviewActivity.this.mToolBar.setVisibility(View.GONE);
            }
        });
        alphaAnimation.setDuration(300);
        this.mBottomBar.startAnimation(alphaAnimation);
        this.mToolBar.startAnimation(alphaAnimation);
        this.mVisible = false;
        this.mHideHandler.removeCallbacks(this.mShowPart2Runnable);
        this.mHideHandler.postDelayed(this.mHidePart2Runnable, 300);
    }

    private void show() {
        if (Build.VERSION.SDK_INT >= 16) {
            SystemUtils.getInstance().systemUiShow(this, this.decorView);
        }
        this.mVisible = true;
        this.mHideHandler.removeCallbacks(this.mHidePart2Runnable);
        this.mHideHandler.post(this.mShowPart2Runnable);
    }

    @Override // com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter.OnClickListener
    public void onPhotoClick() {
        toggle();
    }

    @Override // com.youyi.yychosesdk.ui.adapter.PreviewPhotosAdapter.OnClickListener
    public void onPhotoScaleChanged() {
        if (this.mVisible) {
            hide();
        }
    }

    @Override // android.support.v4.app.FragmentActivity
    public void onBackPressed() {
        doBack();
    }

    private void doBack() {
        Intent intent = new Intent();
        intent.putExtra(Key.PREVIEW_CLICK_DONE, false);
        setResult(this.resultCode, intent);
        finish();
    }

    private void initView() {
        setClick(R.id.iv_back, R.id.tv_edit, R.id.tv_selector);
        this.mToolBar = (FrameLayout) findViewById(R.id.m_top_bar_layout);
        if (!SystemUtils.getInstance().hasNavigationBar(this)) {
            ((FrameLayout) findViewById(R.id.m_root_view)).setFitsSystemWindows(true);
            this.mToolBar.setPadding(0, SystemUtils.getInstance().getStatusBarHeight(this), 0, 0);
            if (ColorUtils.isWhiteColor(this.statusColor)) {
                SystemUtils.getInstance().setStatusDark(this, true);
            }
        }
        this.mBottomBar = (RelativeLayout) findViewById(R.id.m_bottom_bar);
        this.ivSelector = (ImageView) findViewById(R.id.iv_selector);
        this.tvNumber = (TextView) findViewById(R.id.tv_number);
        this.tvDone = (PressedTextView) findViewById(R.id.tv_done);
        this.tvOriginal = (TextView) findViewById(R.id.tv_original);
        this.flFragment = (FrameLayout) findViewById(R.id.fl_fragment);
        this.previewFragment = (PreviewFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_preview);
        if (Setting.showOriginalMenu) {
            processOriginalMenu();
        } else {
            this.tvOriginal.setVisibility(View.GONE);
        }
        setClick(this.tvOriginal, this.tvDone, this.ivSelector);
        initRecyclerView();
        shouldShowMenuDone();
    }

    private void initRecyclerView() {
        this.rvPhotos = (RecyclerView) findViewById(R.id.rv_photos);
        this.adapter = new PreviewPhotosAdapter(this, this.photos, this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, 0, false);
        this.lm = linearLayoutManager;
        this.rvPhotos.setLayoutManager(linearLayoutManager);
        this.rvPhotos.setAdapter(this.adapter);
        this.rvPhotos.scrollToPosition(this.index);
        toggleSelector();
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        this.snapHelper = pagerSnapHelper;
        pagerSnapHelper.attachToRecyclerView(this.rvPhotos);
        this.rvPhotos.addOnScrollListener(new RecyclerView.OnScrollListener() {
            /* class com.youyi.yychosesdk.ui.PreviewActivity.AnonymousClass4 */

            @Override // android.support.v7.widget.RecyclerView.OnScrollListener
            public void onScrollStateChanged(RecyclerView recyclerView, int i) {
                int position;
                super.onScrollStateChanged(recyclerView, i);
                View findSnapView = PreviewActivity.this.snapHelper.findSnapView(PreviewActivity.this.lm);
                if (findSnapView != null && PreviewActivity.this.lastPosition != (position = PreviewActivity.this.lm.getPosition(findSnapView))) {
                    PreviewActivity.this.lastPosition = position;
                    PreviewActivity.this.previewFragment.setSelectedPosition(-1);
                    PreviewActivity.this.tvNumber.setText(PreviewActivity.this.getString(R.string.preview_current_number_easy_photos, new Object[]{Integer.valueOf(PreviewActivity.this.lastPosition + 1), Integer.valueOf(PreviewActivity.this.photos.size())}));
                    PreviewActivity.this.toggleSelector();
                }
            }
        });
        this.tvNumber.setText(getString(R.string.preview_current_number_easy_photos, new Object[]{Integer.valueOf(this.index + 1), Integer.valueOf(this.photos.size())}));
    }

    public void onClick(View view) {
        int id = view.getId();
        if (R.id.iv_back == id) {
            doBack();
        } else if (R.id.tv_selector == id) {
            updateSelector();
        } else if (R.id.iv_selector == id) {
            updateSelector();
        } else if (R.id.tv_original == id) {
            if (!Setting.originalMenuUsable) {
                Toast.makeText(getApplicationContext(), Setting.originalMenuUnusableHint, 0).show();
                return;
            }
            Setting.selectedOriginal = !Setting.selectedOriginal;
            processOriginalMenu();
        } else if (R.id.tv_done == id && !this.clickDone) {
            this.clickDone = true;
            Intent intent = new Intent();
            intent.putExtra(Key.PREVIEW_CLICK_DONE, true);
            setResult(-1, intent);
            finish();
        }
    }

    private void processOriginalMenu() {
        if (Setting.selectedOriginal) {
            this.tvOriginal.setTextColor(ContextCompat.getColor(this, R.color.easy_photos_fg_accent));
        } else if (Setting.originalMenuUsable) {
            this.tvOriginal.setTextColor(ContextCompat.getColor(this, R.color.easy_photos_fg_primary));
        } else {
            this.tvOriginal.setTextColor(ContextCompat.getColor(this, R.color.easy_photos_fg_primary_dark));
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void toggleSelector() {
        if (this.photos.get(this.lastPosition).selected) {
            this.ivSelector.setImageResource(R.drawable.ic_selector_true_easy_photos);
            if (!Result.isEmpty()) {
                int count = Result.count();
                int i = 0;
                while (true) {
                    if (i >= count) {
                        break;
                    } else if (this.photos.get(this.lastPosition).path.equals(Result.getPhotoPath(i))) {
                        this.previewFragment.setSelectedPosition(i);
                        break;
                    } else {
                        i++;
                    }
                }
            }
        } else {
            this.ivSelector.setImageResource(R.drawable.ic_selector_easy_photos);
        }
        this.previewFragment.notifyDataSetChanged();
        shouldShowMenuDone();
    }

    private void updateSelector() {
        this.resultCode = -1;
        Photo photo = this.photos.get(this.lastPosition);
        if (this.isSingle) {
            singleSelector(photo);
        } else if (!this.unable) {
            photo.selected = !photo.selected;
            if (photo.selected) {
                int addPhoto = Result.addPhoto(photo);
                if (addPhoto != 0) {
                    photo.selected = false;
                    if (addPhoto == -3) {
                        Toast.makeText(getApplicationContext(), getString(R.string.selector_single_type_hint_easy_photos), 0).show();
                        return;
                    } else if (addPhoto == -2) {
                        Toast.makeText(getApplicationContext(), getString(R.string.selector_reach_max_video_hint_easy_photos, new Object[]{Integer.valueOf(Setting.complexVideoCount)}), 0).show();
                        return;
                    } else if (addPhoto == -1) {
                        Toast.makeText(getApplicationContext(), getString(R.string.selector_reach_max_image_hint_easy_photos, new Object[]{Integer.valueOf(Setting.complexPictureCount)}), 0).show();
                        return;
                    } else {
                        return;
                    }
                } else if (Result.count() == Setting.count) {
                    this.unable = true;
                }
            } else {
                Result.removePhoto(photo);
                this.previewFragment.setSelectedPosition(-1);
                if (this.unable) {
                    this.unable = false;
                }
            }
            toggleSelector();
        } else if (photo.selected) {
            Result.removePhoto(photo);
            if (this.unable) {
                this.unable = false;
            }
            toggleSelector();
        } else if (Setting.isOnlyVideo()) {
            Toast.makeText(getApplicationContext(), getString(R.string.selector_reach_max_video_hint_easy_photos, new Object[]{Integer.valueOf(Setting.count)}), 0).show();
        } else if (Setting.showVideo) {
            Toast.makeText(getApplicationContext(), getString(R.string.selector_reach_max_hint_easy_photos, new Object[]{Integer.valueOf(Setting.count)}), 0).show();
        } else {
            Toast.makeText(getApplicationContext(), getString(R.string.selector_reach_max_image_hint_easy_photos, new Object[]{Integer.valueOf(Setting.count)}), 0).show();
        }
    }

    private void singleSelector(Photo photo) {
        if (Result.isEmpty()) {
            Result.addPhoto(photo);
        } else if (Result.getPhotoPath(0).equals(photo.path)) {
            Result.removePhoto(photo);
        } else {
            Result.removePhoto(0);
            Result.addPhoto(photo);
        }
        toggleSelector();
    }

    private void shouldShowMenuDone() {
        if (Result.isEmpty()) {
            if (this.tvDone.getVisibility() == 0) {
                ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f);
                scaleAnimation.setDuration(200);
                this.tvDone.startAnimation(scaleAnimation);
            }
            this.tvDone.setVisibility(View.GONE);
            this.flFragment.setVisibility(View.GONE);
            return;
        }
        if (8 == this.tvDone.getVisibility()) {
            ScaleAnimation scaleAnimation2 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
            scaleAnimation2.setDuration(200);
            this.tvDone.startAnimation(scaleAnimation2);
        }
        this.flFragment.setVisibility(View.VISIBLE);
        this.tvDone.setVisibility(View.VISIBLE);
        if (!Result.isEmpty()) {
            if (!Setting.complexSelector || !Setting.complexSingleType) {
                this.tvDone.setText(getString(R.string.selector_action_done_easy_photos, new Object[]{Integer.valueOf(Result.count()), Integer.valueOf(Setting.count)}));
            } else if (Result.getPhotoType(0).contains(Type.VIDEO)) {
                this.tvDone.setText(getString(R.string.selector_action_done_easy_photos, new Object[]{Integer.valueOf(Result.count()), Integer.valueOf(Setting.complexVideoCount)}));
            } else {
                this.tvDone.setText(getString(R.string.selector_action_done_easy_photos, new Object[]{Integer.valueOf(Result.count()), Integer.valueOf(Setting.complexPictureCount)}));
            }
        }
    }

    @Override // com.youyi.yychosesdk.ui.PreviewFragment.OnPreviewFragmentClickListener
    public void onPreviewPhotoClick(int i) {
        String photoPath = Result.getPhotoPath(i);
        int size = this.photos.size();
        for (int i2 = 0; i2 < size; i2++) {
            if (TextUtils.equals(photoPath, this.photos.get(i2).path)) {
                this.rvPhotos.scrollToPosition(i2);
                this.lastPosition = i2;
                this.tvNumber.setText(getString(R.string.preview_current_number_easy_photos, new Object[]{Integer.valueOf(this.lastPosition + 1), Integer.valueOf(this.photos.size())}));
                this.previewFragment.setSelectedPosition(i);
                toggleSelector();
                return;
            }
        }
    }

    private void setClick(int... iArr) {
        for (int i : iArr) {
            findViewById(i).setOnClickListener(this);
        }
    }

    private void setClick(View... viewArr) {
        for (View view : viewArr) {
            view.setOnClickListener(this);
        }
    }
}
